﻿

Imports System.Globalization

''' <summary>
''' 提供IconFont字体项
''' </summary>
Public Class CIconText
    Sub New()
    End Sub
    Sub New(sText As String, sIcon As String)

        Icon = CharFromUnicode(sIcon）
        Text = sText
    End Sub
    Sub New(sText As String, sIcon As String, brush As SolidColorBrush)

        Icon = CharFromUnicode(sIcon） ' character
        Text = sText
        IconBrush = brush
    End Sub
    ''' <summary>
    ''' 字体图标代码
    ''' </summary>
    ''' <returns></returns>
    Property Icon As String
    ''' <summary>
    ''' 字体图标刷子
    ''' </summary>
    ''' <returns></returns>
    Property IconBrush As SolidColorBrush
    ''' <summary>
    ''' 按钮文字
    ''' </summary>
    ''' <returns></returns>
    Property Text As String
    Property Tag As Object

    Public Overrides Function ToString() As String
        Return Text
    End Function

    ''' <summary>
    ''' unicode编码转字符
    ''' </summary>
    ''' <param name="unicode"></param>
    ''' <returns>
    ''' unicode编号: &#xe699;
    ''' '#后接十进制数字，#x后接十六进制数字
    ''' </returns>
    Public Shared Function CharFromUnicode(unicode As String) As Char
        If String.IsNullOrEmpty(unicode) Then Return Nothing
        Dim character As String
        If unicode.StartsWith("&#x") Then
            Dim code As String = unicode.Substring(3)
            If code.EndsWith(";") Then code = code.Remove(code.Length - 1, 1)
            Dim v As Integer = Int32.Parse(code, NumberStyles.HexNumber) ' // 将十六进制编码转换为整型
            character = Char.ConvertFromUtf32(v) ' // 将Unicode编码转换为字符
        ElseIf unicode.StartsWith("&#") Then
            Dim code As String = unicode.Substring(2)
            If code.EndsWith(";") Then code = code.Remove(code.Length - 1, 1)
            Dim v As Integer = Int32.Parse(code) ' // 将10进制编码转换为整型
            character = Char.ConvertFromUtf32(v) ' // 将Unicode编码转换为字符
        Else
            Throw New Exception($"字符串不是Unicode编号格式")
        End If
        Return character
    End Function
End Class

''' <summary>
''' 字典包装，wpf不支持直接泛型
''' </summary>
Public Class CIconTexts
    Inherits Dictionary(Of String, CIconText)
End Class

''' <summary>
''' 当颜色为空时提供默认颜色
''' </summary>
Public Class ConverterIconTextColor
    Implements IValueConverter

    Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.Convert
        If value Is Nothing Then
            Return SystemColors.ControlTextBrush
        Else
            Return value
        End If
    End Function

    Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
        Throw New NotImplementedException()
    End Function
End Class





